#include "PWindowAffineTool.h"


#include <QPainter>
#include <QSize>
#include <QFont>
#include <QPainterPath>
#include <QDebug>
#include <QString>
#include <QTimer>

#include "../IdRootMenu.h"
#include "../vhc/constview4fun.h"

#include "../TViewDraw.h"



namespace kepen {
namespace core {

PWindowAffineTool::PWindowAffineTool(QWidget *parent,int checkedId,QFont &thirdIconFont)
    : QWidget{parent},
    isMouseLeftDown(false),
    m_IdChecked(checkedId),
    m_nIdTouchedDown(MenuId_None),
    m_nIdMouseOver(MenuId_None),thirdFontIcon(thirdIconFont)
{
    int height = HEIGHT_DropCombox_CELL * 4, width = WIDTH_DropCombox_CELL;
    idMenuArray[0] = EditTool_Pan;
    idMenuArray[1] = EditTool_Zoom;
    idMenuArray[2] = EditTool_Rotate;
    idMenuArray[3] = EditTool_Shear;




    // | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint
    setWindowFlags(Qt::Popup);
    // setGeometry(0,0,width,height);
    setFixedSize(width,height);

    setMouseTracking(true);
}

PWindowAffineTool::~PWindowAffineTool()
{

}


void PWindowAffineTool::timeoutClose()
{
    int idTouched = m_nIdTouchedDown;
    Q_EMIT selectedAffineToolSignals(this,idTouched);
}



void PWindowAffineTool::mouseMoveEvent(QMouseEvent *event){
    if(false == isMouseLeftDown){
        QPointF point = event->position();
        int nIdMouseOver = m_nIdMouseOver;
        m_nIdMouseOver = getIdOfTouched(point);
        if(nIdMouseOver != m_nIdMouseOver){
            repaint();
        }
    }
}


void  PWindowAffineTool::mousePressEvent(QMouseEvent *event)
{
    Qt::MouseButton btn = event->button();
    if(Qt::MouseButton::LeftButton == btn)
    {
        QPointF point = event->position();
        ptDown = point;
        isMouseLeftDown = true;
        m_nIdTouchedDown = getIdOfTouched(point);
        m_nIdMouseOver = MenuId_None;
        repaint();
    }
}

void  PWindowAffineTool::mouseReleaseEvent(QMouseEvent *)
{


    bool isMouseDown = isMouseLeftDown;
    // m_nIdTouchedDown = MenuId_None;
    m_nIdMouseOver = MenuId_None;
    isMouseLeftDown = false;
    if(isMouseDown)
    {
        // will be delete this
        int id = m_nIdTouchedDown;
        if(false == (Horizontal_Line_Seperate == id))
            QTimer::singleShot(TimeoutClickFree,this,SLOT(timeoutClose()));
    }

}

void PWindowAffineTool:: paintEvent(QPaintEvent *)
{
    if(isVisible())
    {
        int widthWindow ,heightWidow;
        QSize rectWin = this->size();
        QPainter painter(this);
        painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing |QPainter::VerticalSubpixelPositioning);


        widthWindow  = rectWin.width();
        heightWidow = rectWin.height();

        painter.fillRect(0,0,widthWindow ,heightWidow,QColor(QRgba64::fromArgb32(Background_PopWindow)));

        onPaintButtons(&painter,rectWin);

        QBrush brushBorder(Qt::darkGray,Qt::SolidPattern);
        QPen penBorder(brushBorder,1.0f, Qt::SolidLine);
        painter.setPen(penBorder);
        painter.setBrush(Qt::NoBrush);
        painter.drawRect(0,0,widthWindow ,heightWidow);
    }

}


int  PWindowAffineTool::getIdOfTouched(const QPointF &point)
{
    int x0,y0,nId,nRet,top, bottom, i, numBtns,widthDlg;
    x0 = qRound(point.x());
    y0 = qRound(point.y());
    nRet = MenuId_None;
    bottom = 0;
    numBtns = sizeof(idMenuArray)/sizeof(idMenuArray[0]);
    widthDlg = this->width();

    if(x0 > 0 && x0 < widthDlg){
        for (i = 0; i < numBtns; ++i) {
            top = bottom;
            nId = idMenuArray[i];
            if(Horizontal_Line_Seperate == nId)
                bottom = top + Horizontal_Line_Seperate_Size;
            else
                bottom = top + HEIGHT_DropCombox_CELL;

            if (y0 >= top && y0 < bottom) {
                nRet = nId;
                break;
            }
        }
    }

    return nRet;
}


void  PWindowAffineTool::onPaintButtons(QPainter *painter,const  QSize &rectWin)
{
    int _nID_touchedDown = m_nIdTouchedDown;
    int nIdMouseOver = m_nIdMouseOver;
    int nIdChecked =  m_IdChecked;;
    int i=0,count = sizeof(idMenuArray)/sizeof(idMenuArray[0]);
    int childHeight  ;
    int boolFlag;
    int sizeTextIcon = TextSize_DropCombox_Icon;
    int id,topOfLine = 0,left = 0,childWidth = rectWin.width();
    bool isChecked,isMouseOver,isTouchedDown;
    for(;i<count;++i)
    {
        id = idMenuArray[i];
        childHeight =Horizontal_Line_Seperate == id?Horizontal_Line_Seperate_Size: HEIGHT_DropCombox_CELL;
        isChecked = (id == nIdChecked);
        isMouseOver = (nIdMouseOver == id);
        isTouchedDown = (_nID_touchedDown == id);

        if(Horizontal_Line_Seperate == id)isMouseOver = false;

        boolFlag = isChecked ? BoolFlagChecked : 0;

        boolFlag |= BoolFlagEnable;
        if(isMouseOver)boolFlag |= BoolFlagMouseOver;
        if(isTouchedDown)boolFlag |= BoolFlagTouched;

        if(isChecked)
        {
            QBrush _brushTogBtn(QColor(QRgba64::fromArgb32(Checked_Background)),Qt::SolidPattern);

            painter->fillRect(left, topOfLine, childWidth,childHeight,_brushTogBtn);
        }
        {
            if(isMouseOver)
            {
                if(false == isTouchedDown){
                    quint32 nBackColor = isChecked ?  Checked_Background_Over : Background_PopBtnOverR3;
                    QBrush brushBackOver(QColor(QRgba64::fromArgb32(nBackColor)),Qt::SolidPattern);

                    QBrush brushBorder4Over(QColor(QRgba64::fromArgb32(BorderColor_PopBtnOver)),Qt::SolidPattern);
                    QPen penBorder4Over(brushBorder4Over,Horizontal_Line_Seperate_BorderWidth, Qt::SolidLine);

                    painter->setPen(penBorder4Over);
                    painter->setBrush(brushBackOver);
                    painter->drawRect(left, topOfLine, childWidth,childHeight);

                }
            }
            if(isTouchedDown){
                quint32 nBackColor = isChecked ?  Checked_Background2_Touched:Background_PopBtnDown;
                QBrush brushBackDown(QColor(QRgba64::fromArgb32(nBackColor)),Qt::SolidPattern);
                painter->fillRect(left, topOfLine, childWidth,childHeight,brushBackDown);

            }
        }

        if(EditTool_LineExtrude == id ){
            int cx0, cy0;
            float  outLine []= Coords_EditTool_LineExtrude;
            size_t length = sizeof(outLine)/sizeof(outLine[0]);
            cx0 = left + (childWidth >> 1);
            cy0 = topOfLine + ( childHeight>> 1) +PixDIP;

            TViewDraw::DrawShapeCoords(painter,boolFlag,cx0,cy0,length,outLine);
        }else if(Horizontal_Line_Seperate == id){

            int cy0 = topOfLine + ( childHeight>> 1) ;
            int left = Horizontal_Line_Seperate_Margin,right = WIDTH_DropCombox_CELL - Horizontal_Line_Seperate_Margin;
            QBrush brushSeg(QColor(QRgba64::fromArgb32(Horizontal_Line_Seperate_Color)),Qt::SolidPattern);

            QPen pen(brushSeg,Horizontal_Line_Seperate_BorderWidth, Qt::SolidLine);

            painter->setBrush(Qt::BrushStyle::NoBrush);
            painter->setPen(pen);
            painter->drawLine(left,cy0,right,cy0);

        }
        else{
            QFont &font_t = thirdFontIcon;
            DRAW_ICON_FontR3(left,topOfLine,sizeTextIcon,id)
        }

        if(isChecked)
            TViewDraw::DrawCheckedTriFlag(painter,left, topOfLine, childWidth,childHeight,boolFlag);




        topOfLine += childHeight;
    }
}

}
}
